(function(){
	$.extend($.fn,{
		isEmpty: function(){
			if($.trim($(this).val())!=""){
				return false;
			};
			return true;
		},
		isUrl:  function (){
			var url = $(this).val();
			var strRegex = "^((https|http|ftp|rtsp|mms)?://)"  
				+ "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" 	// ftp的user@  
				+ "(([0-9]{1,3}\.){3}[0-9]{1,3}" 								// IP形式的URL- 199.194.52.184  
				+ "|" 															// 允许IP和DOMAIN（域名）
				+ "([0-9a-z_!~*'()-]+\.)*" 										// 域名- www.  
				+ "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\." 						// 二级域名  
				+ "[a-z]{2,6})" 												// first level domain- .com or .museum  
				+ "(:[0-9]{1,4})?" 												// 端口- :80  
				+ "((/?)|" 														// a slash isn't required if there is no file name  
				+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";  
			var re=new RegExp(strRegex); 
			if (!re.test(url)){
				return false;  
			}  
			return true; 
		}, 
		isChecked: function() {
			if($(this).filter(":checked").length == 0){
				return false;
			}
			return true;
		},
		isEmail: function() {
			var email = $(this).val();
			var re= /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
			if(!re.test(email)) {
				return false;
			}
			return true;
		}
	});
})();


(function(){
	$.extend($.fn,{
		addImgPreview: function(containerId, width, height, debug) {
			if($(this).attr("type") != "file"){
				alert("fail! type!='file'");
			}
			else{
				$(this).bind("change", picCheck);
			};

			function picCheck(){
				var result = imagePreview(this.files, this, containerId, width, height, debug);
				if(!result) {
					$(this).val("");
				}
			}
			function imagePreview(files, file, containerId, width, height, debug) {
				var isSuccess = true;
				var $imgContainer = $("#"+containerId);
				if(!/msie/.test(navigator.userAgent.toLowerCase())) {
					var length = files.length;
					for(i=0; i<length; i++) {
						file = files[i];
						reg = /^image.*$/;
						if(reg.test(file.type)) {
							if(typeof FileReader !== "undefined") {
								fileReader = new FileReader();
								fileReader.onload = function(e){
									var img = document.createElement("img");
                                    img.src = e.target.result;
                                    if(width){
                                    	 img.width = width;
                                    }
                                    if(height){
                                    	img.height = height;
                                    }
                                    $imgContainer.empty().append(img);
								};
								fileReader.readAsDataURL(file);
							} else {
								if(debug) alert("浏览器不支持图片预览 ");
								isSuccess = false;
								break;
							}
						} else {
							if(debug) alert("上传的不是图片");
							isSuccess = false;
						}
					}
				} else {
					file.select();
					// Begin IE9提高了安全性，当input控件为focus时不能使用document.selection.createRange().text获得值，所以加如下语句。
					file.blur();
					var val = document.selection.createRange().text;
					if(/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(val)) {
						$imgContainer.empty();
						$imgContainer.attr("style", "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale);"
								+(width?"width:"+width+"px;":"")
								+(height?"height:"+height+"px;":""));
						$imgContainer[0].filters.item ("DXImageTransform.Microsoft.AlphaImageLoader").src = "file://localhost/"+val;
					} else {
						if(debug) alert("上传的不是图片");
						isSuccess = false;
					}
				}
				return isSuccess;
			}
		}
	});
})();